<style lang="less">
  @import '../styles/common.less';
  @import '../tables/components/table.less';
</style>

<template>
  <div class="meeting-div">
    <Row class="row">
      <Col span="10" class="padding-left-10 list-div">
      <Card>
        <div class="DatePicker-div">
          <DatePicker
              :open="true"
              v-model="DatePickerValue"
              type="date"
              @on-change="handleChange">
            <a href="javascript:void(0)" @click="">
              <template v-if="DatePickerValue === ''"></template>
            </a>
          </DatePicker>
        </div>
      </Card>
      </Col>
      <Col span="14" class="padding-left-10 list-div">
      <Card class="padding-10">
        <div class="table-head">
          <span>{{Datetime}}</span>
          <Button type="primary" class="Button" @click="ExportData()">导出数据</Button>
          <Button type="primary" class="Button" @click="remove()">删除</Button>
          <Button type="primary" class="Button" @click="add()">添加</Button>
        </div>
        <Row class="margin-top-10">
          <Table stripe border :columns="columns" :data="data" @on-row-click="onRowData"
                 @on-selection-change="selectionChange"></Table>
        </Row>
        <div style="margin: 10px;overflow: hidden;height: 36px;">
          <div style="float: right;">
            <Page :total="total" :current="page" @on-change="changePage" show-elevator></Page>
          </div>
        </div>
      </Card>
      </Col>
    </Row>
    <Modal :width="600" title="详情"
           v-model="showCurrentTableData"
           ok-text="确定"
           cancel-text="取消"
           @on-ok=""
           @on-cancel=""
           class-name="vertical-center-modal"
           class="Modal-Detail-div">
      <Form label-position="top">
        <FormItem label="申请人">
          <div class="dataDetail-div" v-text="dataDetail.uname"></div>
        </FormItem>
        <FormItem label="会议室名称" v-if="dataDetail.address && dataDetail.address.name">
          <div class="dataDetail-div" v-text="dataDetail.address.name"></div>
        </FormItem>
        <FormItem label="日期" v-if="dataDetail.day">
          <div class="dataDetail-div" v-text="dataDetail.day"></div>
        </FormItem>
        <FormItem label="开始时间" v-if="dataDetail.start_time">
          <div class="dataDetail-div" v-text="dataDetail.start_time"></div>
        </FormItem>
        <FormItem label="结束时间" v-if="dataDetail.end_time">
          <div class="dataDetail-div" v-text="dataDetail.end_time"></div>
        </FormItem>
        <FormItem label="会议参加人" v-if="meeting_user">
          <div class="dataDetail-div" v-text="meeting_user"></div>
        </FormItem>
        <FormItem label="自动化工作流">
          <div class="dataDetail-div" v-text="dataDetail.is_work==true ? '是' : '否'"></div>
        </FormItem>
        <FormItem label="自动化消息">
          <div class="dataDetail-div" v-text="dataDetail.is_chat==true ? '是' : '否'"></div>
        </FormItem>
        <FormItem label="是否公开会议主题">
          <div class="dataDetail-div" v-text="dataDetail.is_theme_hide==true ? '是' : '否'"></div>
        </FormItem>
        <FormItem label="会议主题" v-if="dataDetail.theme">
          <div class="dataDetail-div" v-text="dataDetail.theme"></div>
        </FormItem>
        <FormItem label="会议纪要" v-if="dataDetail.abstract">
          <div class="dataDetail-div" v-html="dataDetail.abstract"></div>
        </FormItem>

      </Form>
    </Modal>
    <!--编辑-->
    <Modal v-model="addCurrentTableData" :width="600" :mask-closable="false" :closable="false"
           class-name="vertical-center-modal" class="Modal-Detail-div">
      <h3 slot="header" style="color:#2D8CF0">编辑</h3>
      <Form label-position="top">
        <FormItem label="会议室">
          <Select v-model="addData.address_id" :transfer="true" style="width:100%;">
            <Option v-for="item in meeting_room" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </FormItem>
        <FormItem label="日期">
          <DatePicker type="date"
                      format="yyyy-MM-dd"
                      :confirm="true"
                      :transfer="true"
                      :editable="false"
                      placement="bottom-start"
                      placeholder="请选择日期"
                      v-model="addData.DatePickerValue"
                      style="width: 100%;"></DatePicker>
        </FormItem>
        <FormItem label="时间段">
          <TimePicker type="time"
                      format="HH:mm"
                      :confirm="true"
                      :transfer="true"
                      :editable="false"
                      placement="bottom-start"
                      placeholder="开始时间"
                      v-model="addData.startTime"
                      style="width: 45%; float: left;"></TimePicker>
          <div style="width: 10%;text-align: center; float: left;">-</div>
          <TimePicker type="time"
                      format="HH:mm"
                      :confirm="true"
                      :transfer="true"
                      :editable="false"
                      placement="bottom-start"
                      placeholder="结束时间"
                      v-model="addData.endTime"
                      style="width: 45%; float: left;"></TimePicker>
        </FormItem>
        <FormItem label="会议参加人">
          <div class="dataDetail-div">
            <Tree ref="user_tree" :data="TreeData" show-checkbox multiple></Tree>
          </div>
        </FormItem>
        <FormItem label="自动化工作流">
          <i-switch v-model="addData.is_calendar" size="large">
            <span slot="open">是</span>
            <span slot="close">否</span>
          </i-switch>
        </FormItem>
        <FormItem label="自动化消息">
          <i-switch v-model="addData.is_chat" size="large">
            <span slot="open">是</span>
            <span slot="close">否</span>
          </i-switch>
        </FormItem>
        <FormItem label="是否公开会议主题">
          <i-switch v-model="addData.is_theme" size="large">
            <span slot="open">是</span>
            <span slot="close">否</span>
          </i-switch>
        </FormItem>
        <FormItem label="会议主题">
          <Input v-model="addData.theme" type="textarea" class="textarea_resize"
                 :autosize="{minRows: 3}" placeholder="会议主题"></Input>
        </FormItem>
        <FormItem label="会议纪要" v-if="ajaxType == 'update'">
          <Input v-model="addData.abstract" type="textarea" class="textarea_resize"
                 :autosize="{minRows: 3}" placeholder="会议纪要"></Input>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="hideAddModal">取消</Button>
        <Button type="primary" :loading="saveLoading" @click="save()">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import Util from '../../libs/util';
  import Utils from '../../libs/mod.utils';
  import String from '../../libs/mod.string';
  import Modal from '../../libs/mod.Modal';
  export default {
    components: {},
    data () {
      return {
        total: 0,
        page: 1,
        size: 10,
        id: '',
        meeting_user: '',
        searchConName: '',
        searchConTel: '',
        showCurrentTableData: false,
        loading: true,
        showCurrentColumns: [],
        editInlineAndCellData: [],
        columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            key: 'uname',
            title: '申请人',
            minWidth: 100,
          },
          {
            key: 'address',
            title: '会议室',
            minWidth: 100,
            render: (h, params) => {
              return h('div', [
                h('span', params.row.address.name)
              ]);
            }
          },
          {
            key: 'day',
            title: '日期',
            minWidth: 160,
          },
          {
            key: 'start_time',
            title: '开始时间',
            minWidth: 160,
          },
          {
            key: 'end_time',
            title: '结束时间',
            minWidth: 160,
          },
          {
            title: 'Action',
            key: 'action',
            align: 'center',
            minWidth: 100,
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: e => {
                      e.stopPropagation();
                      var data = params.row;
                      this.update(data, params.index);
                    }
                  }
                }, '修改')
              ]);
            }
          }
        ],
        data: [],
        dataDetail: {},
        initTable: [],
        addCurrentTableData: false,
        saveLoading: false,
        ajaxType: 'add',
        addData: {
          address_id: '',
          DatePickerValue: '',//'2018-06-06',
          TimePickerValue: [],//['08:30', '17:30'],
          startTime: '',
          endTime: '',
          is_calendar: false,
          is_chat: false,
          is_theme: false,
          theme: '',
          abstract: '',
        },
        selectionArr: [],
        meeting_room: [],
        TreeData: [],
        checked_dep_arr: [],
        checked_users_arr: [],
        demoEvents: [],
        Datetime: '',
        from: '',
        to: '',
        DatePickerValue: '',
      };
    },
    created () {
      var _this = this;
      var time = new Date();
      var Datetime = Utils.dateToDate(time);
      _this.Datetime = Datetime;
      _this.from = Datetime;
      _this.to = Datetime;
      _this.getData();
      _this.getMettimgRoomData();
    },
    mounted () {
      var _this = this;
      _this.$nextTick(function () {
        $('.table-head').click();
      });
    },
    methods: {
      handleDayChanged (data) {
        var _this = this;
        if (data.date) {
          var Datetime = Utils.dateToDate(data.date);
          _this.Datetime = Datetime;
          _this.from = Datetime;
          _this.to = Datetime;
          _this.getData();
        }
      },
      handleMonthChanged (data) {
        var _this = this;
        var Monthtime = data.replace(/年/g, '-').trim();
        Monthtime = Monthtime.replace(/月/g, '').trim();
        if (Monthtime) {
          //_this.from = Monthtime + '-01 00:00:00';
          //_this.to = Monthtime + '-31 23:59:59';
        }
      },
      handleChange (date) {
        var _this = this;
        _this.DatePickerValue = date;
        _this.Datetime = date;
        _this.from = date;
        _this.to = date;
        _this.getData();
      },
      getData () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          page: _this.page,
          size: _this.size,
          from: _this.from,
          to: _this.to,
        };
        var params = {
          url: '/meeting/list',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.total = res.rows;
            _this.data = _data;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      getMettimgRoomData () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          page: 1,
          size: 100,
        };
        var params = {
          url: '/meeting-room/list',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.meeting_room = _data;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      handleSearch () {
        var _this = this;
        _this.page = 1;
        var searchConName = _this.searchConName;
      },
      changePage (page) {
        var _this = this;
        console.log('page', page);
        _this.page = page;
        _this.getData();
      },
      onRowData: function (data, index) {
        var _this = this;
        _this.show(index, data);
      },
      show: function (index, data) {
        var _this = this;
        var id = data.id;
        _this.id = id;

        var data = {
          id: _this.id,
        };
        var params = {
          url: '/meeting/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            var userArr = [];
            var departments = _data.departments;
            var users = _data.users;
            for (var k in departments) {
              if (!departments[k]) {
                continue;
              }
              var name = departments[k].name;
              userArr.push(name);
            }
            for (var k in users) {
              if (!users[k]) {
                continue;
              }
              var name = users[k].name;
              userArr.push(name);
            }
            _this.meeting_user = userArr.join(' ， ');
            _this.dataDetail = _data;
            _this.showCurrentTableData = true;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      asyncOK: function () {
        var _this = this;
        _this.showCurrentTableData = false;
      },
      // 获取选人 树信息
      getTreeData: function () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          id: 1
        };
        var params = {
          url: '/department/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var data = res.data || [];
            var TreeData = [];
            TreeData.push(data);
            _this.toDataFun(TreeData);
            _this.TreeData = TreeData;
          } else {
            _this.$Message.error('查询人员信息失败!');
          }
        }
      },
      // 处理树信息
      toDataFun: function (arr) {
        var _this = this;
        arr.forEach(function (e) {
          e.title = e.name;
          e.expand = true;//展开
          if (e.type) {
            e.type = 'dep';
            var checked_dep_arr = _this.checked_dep_arr;
            if (Utils.idInArray(e.id, checked_dep_arr)) {
              e.checked = true;// 选中
            }
          } else {
            e.type = 'user';
            var checked_users_arr = _this.checked_users_arr;
            if (Utils.idInArray(e.id, checked_users_arr)) {
              e.checked = true;// 选中
            }
          }
          var staff = e.staff || [];
          var children = e.children || [];
          if (children.length == 0) {
            e.children = [];
          }
          e.children.push(...staff);
          _this.toDataFun(e.children);
        });
      },
      // 列表多选 选中数据
      selectionChange: function (selection) {
        var _this = this;
        _this.selectionArr = selection;
      },
      // 隐藏编辑页面
      hideAddModal: function () {
        var _this = this;
        _this.addCurrentTableData = false;
      },
      // 显示添加界面
      add: function () {
        var _this = this;
        _this.addData.address_id = '';
        _this.addData.DatePickerValue = '';
        _this.addData.startTime = '';
        _this.addData.endTime = '';
        _this.addData.is_calendar = false;
        _this.addData.is_chat = false;
        _this.addData.is_theme = false;
        _this.addData.theme = '';
        _this.addData.abstract = '';
        _this.TreeData = [];
        _this.checked_users_arr = [];
        _this.getTreeData();
        _this.ajaxType = 'add';
        _this.addCurrentTableData = true;
      },
      // 保存数据
      save: function () {
        var _this = this;
        var address_id = _this.addData.address_id;
        var DatePickerValue = _this.addData.DatePickerValue;
        var startTime = _this.addData.startTime;
        var endTime = _this.addData.endTime;
        var is_calendar = _this.addData.is_calendar;
        var is_chat = _this.addData.is_chat;
        var is_theme = _this.addData.is_theme;
        var theme = _this.addData.theme;
        var abstract = _this.addData.abstract;
        var CheckedNodes = _this.$refs.user_tree.getCheckedNodes();

        if (!(address_id && address_id != '')) {
          _this.$Message.error('请选择会议室!');
          return false;
        }
        if (!(DatePickerValue && DatePickerValue != '')) {
          _this.$Message.error('请选择日期!');
          return false;
        }
        if (startTime == '') {
          _this.$Message.error('请选择开始时间!');
          return false;
        }
        if (endTime == '') {
          _this.$Message.error('请选择结束时间!');
          return false;
        }
        if (CheckedNodes.length == 0) {
          _this.$Message.error('请选择会议参加人员!');
          return false;
        }
        DatePickerValue = Utils.dateToDate(DatePickerValue);
        var departments = [];
        var users = [];
        for (var k in CheckedNodes) {
          if (!CheckedNodes[k]) {
            continue;
          }
          var id = CheckedNodes[k].id;
          var type = CheckedNodes[k].type;
          if (type == 'dep') {
            departments.push(id);
          } else if (type == 'user') {
            users.push(id);
          }
        }

        var data = {
          id: _this.id,
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          type: "all",
          meeting_room_id: address_id,
          day: DatePickerValue,
          start_time: startTime,
          end_time: endTime,
          users: users,
          departments: departments,
          is_work: is_calendar,
          is_chat: is_chat,
          is_theme_hide: is_theme,
          theme: theme,
          abstract: abstract
        };
        var url = '/meeting/add';
        if (_this.ajaxType == 'update') {
          url = '/meeting/update';
        }
        var params = {
          url: url,
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        _this.saveLoading = true;
        function _successFun(res) {
          _this.saveLoading = false;
          if (res.result == 0) {
            _this.page = 1;
            _this.getData();
            _this.addData.address_id = '';
            _this.addData.DatePickerValue = '';
            _this.addData.startTime = '';
            _this.addData.endTime = '';
            _this.addData.is_calendar = false;
            _this.addData.is_chat = false;
            _this.addData.is_theme = false;
            _this.addData.theme = '';
            _this.addData.abstract = '';
            _this.TreeData = [];
            _this.checked_users_arr = [];

            _this.addCurrentTableData = false;
          } else {
            _this.$Message.error('保存信息失败!');
          }
        }

      },
      // 显示修改界面
      update: function (data, index) {
        var _this = this;
        _this.ajaxType = 'update';
        var id = data.id;
        _this.id = id;
        var data = {
          id: _this.id,
        };
        var params = {
          url: '/meeting/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            var checked_users_arr = [];
            var departments = _data.departments;
            var users = _data.users;
            for (var k in departments) {
              if (!departments[k]) {
                continue;
              }
              checked_users_arr.push(departments[k]);
            }
            for (var k in users) {
              if (!users[k]) {
                continue;
              }
              checked_users_arr.push(users[k]);
            }
            _this.checked_users_arr = checked_users_arr;
            _this.addData.address_id = _data.address.id;
            _this.addData.DatePickerValue = _data.day;
            _this.addData.startTime = _data.start_time;
            _this.addData.endTime = _data.end_time;
            _this.addData.is_calendar = _data.is_work;
            _this.addData.is_chat = _data.is_chat;
            _this.addData.is_theme = _data.is_theme_hide;
            _this.addData.theme = _data.theme;
            _this.addData.abstract = _data.abstract;

            _this.getTreeData();
            _this.addCurrentTableData = true;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      // 删除
      remove: function () {
        var _this = this;
        var selectionArr = _this.selectionArr;
        if (selectionArr.length == 0) {
          _this.$Message.error('请填选择要删除的数据!');
          return false;
        }
        var selectionIds = [];
        for (var k in selectionArr) {
          if (!selectionArr[k]) {
            continue;
          }
          var id = selectionArr[k].id;
          selectionIds.push(id);
        }
        Modal.confirm(_this, {content: '确定删除选中的信息？'}, function () {
          var data = {
            id: selectionIds,
          };
          var params = {
            url: '/meeting/deleteMultiple',
            type: Util.method.get,
            data: data,
            success: _successFun
          };
          Util.ajax(_this, params);
          function _successFun(res) {
            if (res.result == 0) {
              _this.page = 1;
              _this.getData();
            } else {
              _this.$Message.error('删除信息失败!');
            }
          }
        }, function () {
        });
      },
      // 导出
      ExportData: function () {
        var _this = this;
        var fields = [
          {
            field: 'id',
            title: '#',
            type: 'int',
            order: 1,
          },
          {
            field: 'uname',
            title: '申请人',
            type: 'varchar',
            order: 2,
          },
          {
            field: 'room_name',
            title: '会议室',
            type: 'varchar',
            order: 3,
          },
          {
            field: 'meeting_day',
            title: '日期',
            type: 'datetime',
            order: 4,
          },
          {
            field: 'start_time',
            title: '开始时间',
            type: 'datetime',
            order: 5,
          },
          {
            field: 'end_time',
            title: '结束时间',
            type: 'datetime',
            order: 6,
          },
          {
            field: 'theme',
            title: '会议主题',
            type: 'varchar',
            order: 7,
          },
        ];
        var fieldsStr = Util.toStr(fields);
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          type: 'meeting',
          fields: fieldsStr,
        };
        var params = {
          url: '/file/db2excle',
          type: Util.method.post,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var url = res.data;
            var date = Utils.dateToDateString(new Date());
            Utils.DownloadFile(url, '导出会议室申请信息_' + date + '.xlsx');
          } else {
            _this.$Message.error('导出信息失败!');
          }
        }
      },
    }
  };
</script>
